<?php

require_once './include/common.inc.php';
require_once 'left.php';
require_once 'right.php';

$page_title = 'Acuity World WebShop';
$menu_id = 0;
$menu_str = PrintMenu();

$promoter_menu_id = 2;
$update_succeed = 0;

if (isset($_GET['succeed'])) {
    $update_succeed = 1;
    $promoter_menu_id = 0;
}

$promoter_menu_str = PrintPromoterMenu();

if (isPromoterLogin()) {
    $sql = "SELECT * FROM  `promoter_info` WHERE  `id` = '$promoter_uid'";
    $promoter_info_ary = $db->get_one($sql);
    list($phone1, $phone) = explode("||", $promoter_info_ary['phone']);
    list($mobile1, $mobile) = explode("||", $promoter_info_ary['mobile']);
    $checked1 = $promoter_info_ary['faq1'] == 1 ? "You have chosen training/products for commission." : "You have chosen paid in cash.";
    $checked3 = $promoter_info_ary['faq3'] == 1 ? "checked" : "";
    $checked4 = $promoter_info_ary['faq4'] == 1 ? "checked" : "";
    $checked5 = $promoter_info_ary['faq5'] == 1 ? "checked" : "";
    $checked6 = $promoter_info_ary['faq6'] == 1 ? "checked" : "";
    $checked7 = $promoter_info_ary['faq7'] == 1 ? "checked" : "";
    $checked8 = $promoter_info_ary['faq8'] == 1 ? "checked" : "";
    
    
} else {
    header("LOCATION:promoter_login.php");
    exit;
}

if ($_POST['act'] == "update") {
    
    $email = $_POST['email']; 
    $company = $_POST['company']; 
    $firstname = $_POST['firstname']; 
    $lastname = $_POST['lastname']; 
    $website = $_POST['website']; 
    
    $street = $_POST['street']; 
    $city = $_POST['city']; 
    $state = $_POST['state']; 
    $zip = $_POST['zip']; 
    $country = $_POST['country']; 
    
    $street2 = $_POST['street2']; 
    $city2 = $_POST['city2']; 
    $state2 = $_POST['state2']; 
    $zip2 = $_POST['zip2']; 
    $country2 = $_POST['country2']; 
    
    $phone1 = $_POST['phone1'];
    $phone = $phone1 . "||" . $_POST['phone'];
    $mobile1 = $_POST['mobile1'];
    $mobile = $mobile1 . "||" . $_POST['mobile'];
    
    
    $b_bank = $_POST['bank'];
    $b_registration = $_POST['registration'];
    $b_account = $_POST['account'];
    $b_iban = $_POST['iban'];
    $b_swift = $_POST['swift'];   
    
    $faq3 = isset($_POST['faq3']) && $_POST['faq3'] == 1 ? 1 : 0;
    $faq4 = isset($_POST['faq4']) && $_POST['faq4'] == 1 ? 1 : 0;
    $faq5 = isset($_POST['faq5']) && $_POST['faq5'] == 1 ? 1 : 0;
    $faq6 = isset($_POST['faq6']) && $_POST['faq6'] == 1 ? 1 : 0;
    $faq7 = isset($_POST['faq7']) && $_POST['faq7'] == 1 ? 1 : 0;
    $faq8 = isset($_POST['faq8']) && $_POST['faq8'] == 1 ? 1 : 0;
    
    if (trim($email) == "" || trim($firstname) == "" || trim($lastname) == "" ||
    trim($street) == "" || trim($city) == "" || trim($state) == "" || trim($zip) == "" || trim($country) == "" || trim($phone1) == "" || trim($phone) == "" ) {
        echo "<script>alert('Fields marked with an asterisk * are required');history.back();</script>";
        exit;
    }
    
    
    if (!preg_match("/[a-z0-9\.]{2,}@[a-z0-9]{2,}\.[a-z0-9]+/", $email)) {
        echo "<script>alert('Invalid email, Please try again');history.back();</script>";
        exit;
    }
    
    // жemailΨһ
    $sql = "SELECT `id` FROM `promoter_info` WHERE `id` != '$promoter_uid' AND `email` = '$email'";
    $rs = $db->get_one($sql);
    if (!empty($rs)) {
        echo "<script>alert('Email exist.');history.back(1);</script>";
        exit();
    }
    
    
    $sql = "update `promoter_info` set  
            `company`='$company' , 
            `firstname`='$firstname' , 
            `lastname`='$lastname', 
            `street`='$street', 
            `city`='$city', 
            `state`='$state', 
            `zip`='$zip', 
            `country`='$country', 
            `street2`='$street2', 
            `city2`='$city2', 
            `state2`='$state2', 
            `zip2`='$zip2', 
            `country2`='$country2', 
            `phone`='$phone', 
            `mobile`='$mobile' , 
            `email`='$email' , 
            `website`='$website' , 
            `b_bank`='$b_bank' , 
            `b_registration`='$b_registration' , 
            `b_account`='$b_account' , 
            `b_iban`='$b_iban' , 
            `b_swift`='$b_swift' ,
            `faq3`='$faq3' ,
            `faq4`='$faq4' ,
            `faq5`='$faq5' ,
            `faq6`='$faq6' ,
            `faq7`='$faq7' ,
            `faq8`='$faq8'
            WHERE  `id` = '$promoter_uid' LIMIT 1
            ";
    
    $db->query($sql);
    header("location:promoter_info_preview.php?succeed");
    exit;
}


require_once PrintEot('header');
require_once PrintEot('left');
require_once PrintEot('promoter_info_preview');
require_once PrintEot('right');
require_once PrintEot('footer');

?>